package com.umgsai.thread.thread52;

import java.util.concurrent.locks.ReentrantLock;

//getQueueLength返回正在等待此锁定的线程估计数
public class MyService {
	public ReentrantLock lock = new ReentrantLock();
	public void serviceMethod1() {
		try {
			lock.lock();
			System.out.println(Thread.currentThread().getName() + "进入方法");
			Thread.sleep(Integer.MAX_VALUE);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	public static void main(String[] args) throws InterruptedException {
		final MyService myService = new MyService();
		Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				myService.serviceMethod1();
			}
		};
		Thread[] threads = new Thread[10];
		for (int i = 0; i < threads.length; i++) {
			threads[i] = new Thread(runnable);
		}
		for (int i = 0; i < threads.length; i++) {
			threads[i].start();
		}
		Thread.sleep(2000);
		System.out.println(myService.lock.getQueueLength() + "在等待获取锁");
	}
}
